package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.harmontronics.hanma.paas.paas.annotation.Log;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmEntityModel;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmModel;
import com.harmontronics.hanma.paas.paas.dto.input.model.HmEntityModelCreateInput;
import com.harmontronics.hanma.paas.paas.dto.input.model.HmEntityModelUpdateInput;
import com.harmontronics.hanma.paas.paas.dto.output.HmEntityModelResult;
import com.harmontronics.hanma.paas.paas.service.HmEntityModelAppService;
import com.harmontronics.hanma.paas.paas.service.IHmEntityModelAppService;
import com.harmontronics.hanma.paas.paas.service.IHmModelAppService;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;
import com.harmontronics.hanma.paas.paas.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;


/**
 * 实体模型
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-03-31 09:10:30
 */
@RestController
@RequestMapping("/api/hmentitymodel")
@Api(tags = {"实体模型"})
public class HmEntityModelController {
    @Autowired
    private IHmEntityModelAppService hmEntityModelappService;

    @Autowired
    private IHmModelAppService hmModelappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmEntityModelResult>> list(@ApiParam(value = "模型ID") @RequestParam(required = false) Long modelId,
                                                          @ApiParam(value = "模型Key") @RequestParam(required = false) String modelKey,
                                                          @RequestParam(required = false) Boolean activated) {
        HmModel hmModel = null;
        if(modelId != null){
            hmModel = hmModelappService.findById(modelId);
        }else if(StringUtil.isNotBlank(modelKey)){
            hmModel = hmModelappService.findByCode(modelKey);
        }
        final String modelCode = hmModel.getCode();
        List<HmEntityModel> hmEntityModelList = hmEntityModelappService.getList(modelId,modelKey,activated);
        if(CollectionUtil.isNotEmpty(hmEntityModelList)){
            hmEntityModelList.forEach(item->{
                item.setModelCode(modelCode);
            });
        }
        return ResponseEntity.ok(Convert.toList(HmEntityModelResult.class, hmEntityModelList));
    }

    /**
     * 分页列表
     */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmEntityModelResult>> list(
            @ApiParam(value = "模型ID") @RequestParam Long modelId,
            @ApiParam(value = "是否隐藏系统字段") @RequestParam(required = false,defaultValue = "false") boolean hiddenSystemField,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmEntityModel> hmEntityModelPageBase = hmEntityModelappService.getList(modelId,hiddenSystemField, pageNo, pageSize);
        PageBase<HmEntityModelResult> resultPageBase = PageBase.copy(hmEntityModelPageBase, HmEntityModelResult.class);
        return ResponseEntity.ok(resultPageBase);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmEntityModelResult> info(@RequestParam("id") Long id) {
        HmEntityModel hmEntityModel = hmEntityModelappService.findById(id);
        HmEntityModelResult result = BeanUtil.toBean(hmEntityModel, HmEntityModelResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    @Log("新增了模型字段")
    public ResponseEntity<String> save(@Valid @RequestBody HmEntityModelCreateInput hmEntityModelInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmEntityModel hmEntityModel = BeanUtil.toBean(hmEntityModelInput, HmEntityModel.class);
        hmEntityModel.setBuiltIn(Boolean.FALSE);
        hmEntityModelappService.save(hmEntityModel);
        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    @Log("修改了模型字段")
    public ResponseEntity<String> update(@Valid @RequestBody HmEntityModelUpdateInput hmEntityModelInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmEntityModel hmEntityModel = BeanUtil.toBean(hmEntityModelInput, HmEntityModel.class);
        hmEntityModel.setBuiltIn(Boolean.FALSE);
        hmEntityModelappService.update(hmEntityModel);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    @Log("删除了模型字段")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmEntityModelappService.remove(Arrays.asList(ids));

        return ResponseEntity.ok();
    }


    @GetMapping("valid-field")
    @ApiOperation("字段重复检查")
    public ResponseEntity<String> validField(@RequestParam String key, @RequestParam String value, @RequestParam(required = false) Long id, @RequestParam Long modelId) {
        hmEntityModelappService.validField(id, key, value, modelId);
        return ResponseEntity.ok();
    }


}
